/**
 * 
 */
package mumu.android.app;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;


public class zLib_bindservice extends Service
{
	private int count;
	private boolean quit;
	// 定义onBinder方法所返回的对象
	private MyBinder binder = new MyBinder();
	// 通过继承Binder来实现IBinder类
	public class MyBinder extends Binder
	{
		public int getCount()
		{
			// 获取Service的运行状态：count
			return count;
		}
	}
	// 必须实现的方法
	@Override
	public IBinder onBind(Intent intent)
	{
		Toast.makeText(getApplicationContext(), "Service is Binded", 1000).show();
		// 返回IBinder对象
		return binder;
	}
	// Service被创建时回调该方法。
	@Override
	public void onCreate()
	{
		super.onCreate();
		Toast.makeText(getApplicationContext(), "Service is Created", 1000).show();
		// 启动一条线程、动态地修改count状态值
		new Thread()
		{
			@Override
			public void run()
			{
				while (!quit)
				{
					try
					{
						Thread.sleep(2000);
					}
					catch (InterruptedException e)
					{
					}
					count++;
					//Toast.makeText(getApplicationContext(), "Service "+count, 1000).show();
				}
			}
		}.start();		
	}
	// Service被断开连接时回调该方法
	@Override
	public boolean onUnbind(Intent intent)
	{
		Toast.makeText(getApplicationContext(), "Service is Unbinded", 1000).show();
		return true;
	}
	// Service被关闭之前回调。
	@Override
	public void onDestroy()
	{
		super.onDestroy();
		this.quit = true;
		Toast.makeText(getApplicationContext(), "Service is Destroyed", 1000).show();
	}
	
	@Override
	public void onRebind(Intent intent) 
	{
		super.onRebind(intent);
		this.quit = true;
		Toast.makeText(getApplicationContext(), "Service is ReBinded", 1000).show();
	}	
}
